﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;

using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Resources.Admin;
using SIG.Model.Ads;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;

namespace SIG.Service
{
    public class CarouselService: EntityService<Carousel>, ICarouselService
    {
     
        private readonly ICarouselRepository _carouselRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public CarouselService(ICarouselRepository carouselRepository,ILoggingService loggingService, ICacheService cacheService)
            : base(carouselRepository, cacheService, loggingService)
        {
           
            _carouselRepository = carouselRepository;       
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public IEnumerable<Carousel> GetCarouselsByPositionId(int positionId)
        {

            //get list
            var key = $"{EntityNames.Carousel}s_{positionId}";

            List<Carousel> carousels;

            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    carousels = (List<Carousel>)_cacheService.Get(key);

                }
                else
                {
                    carousels = _carouselRepository.GetFilteredElements(m => m.Active && m.PositionId == positionId, m => m.Position).ToList();               
                    _cacheService.Set(key, carousels, SettingsManager.Ads.CacheDuration);
                }
            }
            else
            {
                carousels = _carouselRepository.GetFilteredElements(m => m.Active && m.PositionId == positionId, m => m.Position).ToList();
                
            }

            return carousels;

        }
        public Carousel GetByIdWithPosition(int Id)
        {

            //get list
            var key = $"{EntityNames.Carousel}_{Id}";

            Carousel carousel;

            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    carousel = (Carousel)_cacheService.Get(key);

                }
                else
                {
                    carousel = _carouselRepository.GetFirstOrDefault(m=>m.Id == Id, m => m.Position);
                    _cacheService.Set(key, carousel, SettingsManager.Ads.CacheDuration);
                }
            }
            else
            {
                carousel = _carouselRepository.GetFirstOrDefault(m => m.Id == Id, m => m.Position);
            }

            return carousel;

        }

        public IEnumerable<Carousel> GetActiveCarousels()
        {
            //get list
            var key = $"{EntityNames.Carousel}_ActiveList";

            List<Carousel> carousels;
            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    carousels = (List<Carousel>)_cacheService.Get(key);

                }
                else
                {
                    carousels = _carouselRepository.GetFilteredElements(c=>c.Active).ToList();
                    _cacheService.Set(key, carousels, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                carousels = _carouselRepository.GetFilteredElements(c => c.Active).ToList();
            }

            return carousels;
        }


        public IEnumerable<Carousel> GetCarousels(int PageIndex,int PageSize,out int CarouselCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Carousel}_ListCount";
            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    CarouselCount = (int)_cacheService.Get(keyCount);

                }
                else
                {
                    CarouselCount = _carouselRepository.GetAllElements().Count();
                    _cacheService.Set(keyCount, CarouselCount, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                CarouselCount = _carouselRepository.GetAllElements().Count();
            }


            return GetCarousels(PageIndex, PageSize);


        }

        public IEnumerable<Carousel> GetCarousels(int PageIndex, int PageSize)
        {
            //get list
            var key = $"{EntityNames.Carousel}_List_{PageIndex}_{PageSize}";

            List<Carousel> carousels;
            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    carousels = (List<Carousel>)_cacheService.Get(key);

                }
                else
                {
                    carousels = _carouselRepository.GetPagedElements(PageIndex, PageSize, (c => c.Importance), false).ToList();
                    _cacheService.Set(key, carousels, SettingsManager.Ads.CacheDuration);
                }
            }
            else
            {
                carousels = _carouselRepository.GetPagedElements(PageIndex, PageSize, (c => c.Importance), false).ToList();
            }

            return carousels;
        }


        public List<Carousel> GetPagedElements(int pageIndex, int pageSize, string keyword, int? positionId, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Carousel}_ListCount_{keyword}_{positionId}";

            var totalIQuery = _carouselRepository.GetAllElements();
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));
            if (positionId > 0)
                totalIQuery = totalIQuery.Where(g => g.PositionId == positionId);

            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Ads.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Carousel}_List_{pageIndex}_{pageSize}_{keyword}_{positionId}";

            List<Carousel> carousels;
            Expression<Func<Carousel, bool>> filter = g => true;
            Expression<Func<Carousel, bool>> filterByKeyword = g => g.Title.Contains(keyword);
            Expression<Func<Carousel, bool>> filterByAlbum = g => g.PositionId == positionId;

            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);
            if (positionId > 0)
                filter = filter.AndAlso(filterByAlbum);


            if (SettingsManager.Ads.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    carousels = (List<Carousel>)_cacheService.Get(key);
                }
                else
                {
                    carousels = _carouselRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false, g => g.Position).ToList();
                    _cacheService.Set(key, carousels, SettingsManager.Ads.CacheDuration);
                }
            }
            else
            {
                carousels = _carouselRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false, g => g.Position).ToList();
            }

            return carousels;

        }

    }
}
